//
//  SelectRelationViewController.m
//  EducationAdminClient
//
//  Created by kwsdzjx on 16/8/5.
//  Copyright © 2016年 周佳兴. All rights reserved.
//

#import "SelectRelationViewController.h"
#import "DLScrollTabbarView.h"
#import "DLLRUCache.h"
#import "SelectRelecationAddViewController.h"
#import "DLCustomSlideView.h"

#import "ListsViewController.h" // 列表页 循环引用

@interface SelectRelationViewController ()<DLCustomSlideViewDelegate>{
    NSMutableArray *itemArray_;

}
@property (strong, nonatomic)DLCustomSlideView *slideView;
@property (strong, nonatomic)NSMutableDictionary    *catchDic;

@end

@implementation SelectRelationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _catchDic = [[NSMutableDictionary alloc]init];

    //*** createLeftNavBar
    [self createLeftNavBar];
    
    //*** createUI
    [self createUI];

}

#pragma mark - createUI
-(void)createUI{
    self.automaticallyAdjustsScrollViewInsets = NO; // 如果你使用了UITabBarController, 系统会自动调整scrollView的inset。加上这个如果出错的话。
    
    
    self.slideView = [[DLCustomSlideView alloc]init];
    self.slideView.delegate = self;
    
    [self.view addSubview:self.slideView];
    [self.slideView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.top.equalTo(self.view.mas_top);
        make.bottom.equalTo(self.view.mas_bottom);
        make.right.equalTo(self.view.mas_right);
    }];
    // Do any additional setup after loading the view from its nib.
    DLLRUCache *cache = [[DLLRUCache alloc] initWithCount:6];
    DLScrollTabbarView *tabbar = [[DLScrollTabbarView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 90/2)];
    //*** 上面选项卡背景颜色，没设置背景图
    tabbar.backgroundColor = [UIColor whiteColor];
    tabbar.tabItemNormalColor = [UIColor colorWithHex:0x858585];
    tabbar.tabItemSelectedColor = [UIColor colorWithHex:0x5da6ff];
    tabbar.tabItemNormalFontSize = 16.0f;
    tabbar.trackColor = [UIColor colorWithHex:0x5da6ff];
    tabbar.trackLongColor = [UIColor colorWithHex:0xe6e6e6];
    itemArray_ = [NSMutableArray array];
   
    DLScrollTabbarItem *item;
    if(_segmentArr.count>0){
    }else{
        item = [DLScrollTabbarItem itemWithTitle:[NSString stringWithFormat:@" "] width:80];
        [itemArray_ addObject:item];

    }
    NSMutableArray *arrNoTitle = [[NSMutableArray alloc]init];

    for (int i=0; i<_segmentArr.count; i++) {
        
        NSDictionary *dicSeg = _segmentArr[i];
        [arrNoTitle addObject:[NSString stringWithFormat:@"%@", [dicSeg valueForKey:@"childPageName"]]];
        NSString *string = [NSString stringWithFormat:@"%@", [dicSeg valueForKey:@"childPageName"]];
        
        CGSize size =  [string boundingRectWithSize:CGSizeMake(999, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:32/2]} context:nil].size;

        item = [DLScrollTabbarItem itemWithTitle:string width:size.width+24/2+30/2];
        [itemArray_ addObject:item];
    }
    tabbar.tabbarItems = itemArray_;
    
    self.slideView.tabbar = tabbar;
    self.slideView.cache = cache;
    self.slideView.tabbarBottomSpacing = 5;
    self.slideView.baseViewController = self;
    [self.slideView setup];
    self.slideView.selectedIndex = 0;

}

- (NSInteger)numberOfTabsInDLCustomSlideView:(DLCustomSlideView *)sender{
    return _segmentArr.count; // 明天再改吧  太卡 我擦
}

- (UIViewController *)DLCustomSlideView:(DLCustomSlideView *)sender controllerAt:(NSInteger)index{
     ListsViewController* ctrl = [[ListsViewController alloc] init];
    
    NSString *strCatchIndex = [NSString stringWithFormat:@"%ld", (long)index];
    [_catchDic setObject:ctrl forKey:strCatchIndex];

    ctrl.relationDataDic = _segmentArr[index];
    
    ctrl.mainId = _mainId;
    ctrl.mainTableId = _mainTableId;
    ctrl.mainPageId = _mainPageId;
//    ctrl.segmentArr = _segmentArr;
//    ctrl.currentIndex = index;
    return ctrl;
}

-(void)DLCustomSlideView:(DLCustomSlideView *)sender didSelectedAt:(NSInteger)index{
    
    NSString *strCatchIndex = [NSString stringWithFormat:@"%ld", (long)index];
    if ([_catchDic objectForKey:strCatchIndex]) {
        ListsViewController *listVC = (ListsViewController *)[_catchDic objectForKey:strCatchIndex];
        listVC.relationDataDic = _segmentArr[index];
        
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
